package top.lunarye.modules.staff.entity;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;

/**
 * @description:
 * @author: lunar
 * @since: 2021-07-16
 */
public enum RoleEnum {
    // 学生角色枚举
    student(1, "student"),
    teacher(2, "teacher"),
    admin(3, "admin");

    @EnumValue
    private final Integer id;
    private final String roleName;

    public Integer getId() {
        return id;
    }

    public String getRoleName() {
        return roleName;
    }

    /**
     * 使用id获取roleName
     * @param roleId
     * @return
     */
    public static String getRole(Integer roleId) {
        RoleEnum[] roleEnums = values();
        for (RoleEnum role: roleEnums) {
            if (role.getId().equals(roleId)) {
                return role.getRoleName();
            }
        }
        return null;
    }

    public static RoleEnum getRoleEnum(Integer roleId) {
        Class<RoleEnum> roleClass = RoleEnum.class;
        RoleEnum[] roles = roleClass.getEnumConstants();

        for (RoleEnum role: roles) {
            if (role.getId().equals(roleId)) {
                return role;
            }
        }
        return null;
    }

    public static RoleEnum getRoleEnum(String roleName) {
        Class<RoleEnum> roleClass = RoleEnum.class;
        RoleEnum[] roles = roleClass.getEnumConstants();

        for (RoleEnum role: roles) {
            if (role.getRoleName().equals(roleName)) {
                return role;
            }
        }
        return null;
    }

    @JsonCreator
    RoleEnum(int id, String roleName) {
        this.id = id;
        this.roleName  = roleName;
    }
}
